package com.it.controller;

import com.it.pojo.Books;
import com.it.pojo.Category;
import com.it.pojo.Result;
import com.it.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BooksController {
    @Autowired
    private BooksService booksService;

    @GetMapping
    public Result<List<Books>> list(){
        List<Books>  b =  booksService.list();
        System.out.println("后端返回书籍数据：" + b);
        return Result.success(b);
    }

    @PutMapping
    public Result update(@RequestBody @Validated(Books.Update.class) Books books){
        booksService.update(books);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody @Validated(Books.add.class) Books books){
        System.out.println("Received category: " + books);
        booksService.add(books);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteCategory(@RequestParam Integer id){
        booksService.delete(id);
        return  Result.success();
    }
}
